package org.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.example.common.R;
import org.example.common.StatusCode;
import org.example.interceptor.UserHolder;
import org.example.mapper.*;
import org.example.pojo.entity.ApConcern;
import org.example.pojo.entity.ApLove;
import org.example.pojo.entity.ApUser;
import org.example.pojo.entity.WmNews;
import org.example.pojo.vo.LoginVo;
import org.example.pojo.vo.PersonVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.service.IApUserService;
import org.example.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-26
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {

    @Autowired
    private ApLoveMapper apLoveMapper;

    @Autowired
    private ApCollectMapper apCollectMapper;

    @Autowired
    private ApConcernMapper apConcernMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public R login(ApUser apUser) {
        QueryWrapper<ApUser> wrapper = new QueryWrapper<ApUser>();
        wrapper.eq("phone", apUser.getPhone());
        ApUser user = baseMapper.selectOne(wrapper);
        if (user == null) {
            return R.error(StatusCode.USER_NOT_EXIST);
        }
        //用户存在，校验密码
        if (!DigestUtils.md5Hex(apUser.getPassword()).equals(user.getPassword())) {
            return R.error(StatusCode.PASSWORD_ERROR);
        }

        //生成token
        String token = JWTUtils.createToken(String.valueOf(user.getId()), user.getPhone());
        LoginVo loginVo = new LoginVo();
        BeanUtil.copyProperties(user, loginVo);
        loginVo.setToken(token);

        return R.ok(loginVo);
    }

    @Override
    public R register(ApUser apUser) {
        QueryWrapper<ApUser> wrapper = new QueryWrapper<ApUser>();
        wrapper.eq("phone", apUser.getPhone());
        ApUser user = baseMapper.selectOne(wrapper);
        if (user != null) {
            return R.error(StatusCode.PHONE_ALREADY_EXIST);

        }
        //md5加密
        String pwd = DigestUtils.md5Hex(apUser.getPassword());
        apUser.setPassword(pwd);
        apUser.setName("用户" + apUser.getPhone());
        apUser.setImage("https://animal-s.oss-cn-beijing.aliyuncs.com/2025/02/20/3ccd7329-cced-40fc-b116-805e127668d9.jpg");
        baseMapper.insert(apUser);
        return R.ok("注册成功");
    }

    @Override
    public PersonVo info() {
        //查询获赞数量
        //先查询当下所有文章
        QueryWrapper<WmNews> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", UserHolder.getUserId());
        List<WmNews> wmNews = wmNewsMapper.selectList(wrapper);
        Long likeNum = 0L;
        for (WmNews wmNew : wmNews) {
            //查询喜欢的数量
            QueryWrapper<ApLove> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("article_id", wmNew.getId());
            Long count = apLoveMapper.selectCount(wrapper1);
            likeNum += count;
        }
        // 获取粉丝数量
        QueryWrapper<ApConcern> fansWrapper = new QueryWrapper<>();
        fansWrapper.eq("concern_user_id", UserHolder.getUserId());
        Long fansNum = apConcernMapper.selectCount(fansWrapper);

// 获取关注量
        QueryWrapper<ApConcern> focusWrapper = new QueryWrapper<>();
        focusWrapper.eq("user_id", UserHolder.getUserId());
        Long focusOnNum = apConcernMapper.selectCount(focusWrapper);


        //获取图片
        ApUser user = baseMapper.selectById(UserHolder.getUserId());


        PersonVo personVo = new PersonVo();
        personVo.setLikeNum(likeNum);
        personVo.setFocusOnNum(focusOnNum);
        personVo.setFansNum(fansNum);
        personVo.setImageUrl(user.getImage());

        return personVo;
    }


}
